package analysis;

import model.ApiConfigDataModel;
import model.TestResultAnalysisModel;
import tool.DataReplace;

/**
 * Created by yangxianming on 2015-06-17.
 */
public class ExpectComparison implements AnalysisImpl{
    String comparisonWord[] = new String[]{"\"!=\"", "\"==\"", "\"<\"", "\">\"", "\"=>\"", "\">=\"", "\"=<\"", "\"<=\""};
    String expectSplit[] = new String[]{"", ""};
    String expect = null;

    ExpectComparison(String expect){
        this.expect = expect;
    }
    @Override
    public void parseExpect(TestResultAnalysisModel testResultAnalysisModel, ApiConfigDataModel acdm) {
        expect = expect.substring("comparison-".length());
        for (String cw:comparisonWord){
            if (expect.contains(cw)){
                expectSplit = expect.split(cw);
                expectSplit[0] = expectSplit[0].trim();
                expectSplit[1] = expectSplit[1].trim();
                testResultAnalysisModel.result = comparison(expectSplit, cw, acdm, testResultAnalysisModel);
            }
        }
    }

    private boolean comparison(String[] expectSplit, String cw, ApiConfigDataModel acdm, TestResultAnalysisModel testResultAnalysisModel){

        String describe = "test value:" + acdm.config.get(expectSplit[0]) + ", expect is:" + expectSplit[1];
//        如果在config中有值那就替换,否则就用原值
        expectSplit[0] = DataReplace.replace(acdm, expectSplit[0]);
//        if (acdm.config.get(expectSplit[0]) != null){
//            expectSplit[0] = acdm.config.get(expectSplit[0]);
//        }
//        if ((expectSplit[0] = acdm.config.get(expectSplit[0])) == null)
//            return false;

        if (cw.contains("!=")){
            if (expectSplit[0].equals(expectSplit[1])) {
//                保存测试结果
                testResultAnalysisModel.describe = describe;
                return false;
            }
        }

        if (cw.contains("==")){
            if (!expectSplit[0].equals(expectSplit[1])){
                testResultAnalysisModel.describe = describe;
                return false;
            }
        }

        if (cw.contains("<") || cw.contains("<=") || cw.contains("=<")) {
            Long a0 = Long.valueOf(expectSplit[0]);
            Long a1 = Long.valueOf(expectSplit[1]);
            if (a0 > a1) {
                testResultAnalysisModel.describe = describe;
                return false;
            }
        }

        if (cw.contains(">") || cw.contains(">=") || cw.contains("=>")){
            Long a0 = Long.valueOf(expectSplit[0]);
            Long a1 = Long.valueOf(expectSplit[1]);
            if (a0 < a1) {
                testResultAnalysisModel.describe = describe;
                return false;
            }
        }
        testResultAnalysisModel.describe = describe;
        return true;
    }
}
